<template>
  <div class="banner-page">
    <el-button type="primary" @click="dialogVisible = true">添加轮播图</el-button>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="编号" width="300px"></el-table-column>
      <el-table-column prop="image" label="图片">
        <template v-slot="scope">
          <el-image
            style="width: 300px; height: 160px"
            :src="scope.row.image"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.state"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            disabled
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column width="180" label="操作">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row.id)" size="small" type="primary">编辑</el-button>
          <el-button @click="del(scope.row.id)" size="small" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>

    <!-- 轮播图添加 -->
    <el-dialog title="轮播图添加" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="bannerInfo"
        ref="bannerInfo"
        label-width="100px"
        class="demo-dynamic"
      >
        <el-form-item
          prop="state"
          label="轮播图状态"
          :rules="[{ required: true, message: '请输入轮播图状态' }]"
        >
          <el-input type="number" v-model="bannerInfo.state"></el-input>
        </el-form-item>
        <el-form-item
          prop="image"
          label="轮播图"
          :rules="[{ required: true, message: '请上传轮播图' }]"
        >
          <el-upload
            class="avatar-uploader"
            action="http://8.140.198.52:8001/common/uploadFile"
            enctype="multipart/form-data"
            method="post"
            :headers="header"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img style="width: 300px; height: 200px;" v-if="bannerInfo.image" :src="bannerInfo.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { bannerAll, delBanner, addBanner } from "@/api/users";
export default {
  data() {
    return {
      dialogVisible: false,
      header: {
        Authorization: sessionStorage.getItem("token"),
      },
      bannerList: [],
      bannerInfo: {
        image: "",
        state: 0,
      },
      // 当前页码，初始化为1
      currentPage: 1,
      // 每页显示的条数，默认设置为10，可选项还有20
      pageSize: 10,
      // 总数据条数，初始为0，后续根据接口返回数据更新
      total: 0,
      // 用于表格展示的当前页数据
      tableData: []
    };
  },
  created() {
    this.getBannerList();
  },
  methods: {
    // 添加上传图片
    handleAvatarSuccess(pic) {
      // console.log(pic);
      this.bannerInfo.image = pic;
    },
    async getBannerList() {
      const res = await bannerAll();
      // console.log(res);
      if (res.status === 0) {
        this.bannerList = res.bannerList;
        this.total = this.bannerList.length;
        this.getTableData();
      }
    },
    // 根据当前页码和每页显示条数获取当前页数据
    getTableData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.tableData = this.bannerList.slice(start, end);
    },
    // 每页条数改变时的处理函数
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getTableData();
    },
    // 当前页码改变时的处理函数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTableData();
    },
    // 修改模块
    edit(id) {
      console.log(id);
    },
    // 删除模块
    del(id) {
      // console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
       .then(async () => {
          const res = await delBanner(id);
          if (res.status === 0) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getBannerList();
          }
        })
       .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    sub() {
      // console.log(this.bannerInfo);
      addBanner(this.bannerInfo).then(res => {
        // console.log(res);
        this.dialogVisible = false;
        this.getBannerList();
      });
    }
  },
};
</script>

<style scoped>
/* 整体页面容器样式 */
.banner-page {
  padding: 20px;
  background-color: #f8f9fa;
}

/* 按钮样式 */
.el-button {
  margin-bottom: 10px;
  border-radius: 4px;
  padding: 6px 12px;
  transition: all 0.3s ease;
}
.el-button:hover {
  opacity: 0.8;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

/* 表格样式 */
.el-table {
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}
.el-table th {
  background-color: #f0f0f0;
  color: #333;
  font-weight: normal;
  text-align: center;
  padding: 12px 0;
}
.el-table td {
  text-align: center;
  padding: 10px 0;
}
.el-table.el-image {
  margin: 0 auto;
}
.el-table.el-switch {
  margin: 0 auto;
}
.el-table.el-button {
  margin: 0 5px;
  padding: 6px 12px;
  border-radius: 3px;
  transition: all 0.3s ease;
}
.el-table.el-button:hover {
  opacity: 0.8;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

/* 分页组件样式 */
.el-pagination {
  text-align: center;
  margin-top: 10px;
}
.el-pagination.is-background.el-pager li {
  background-color: #f0f0f0;
  color: #333;
  border-radius: 3px;
  margin: 0 3px;
  transition: all 0.3s ease;
}
.el-pagination.is-background.el-pager li:hover {
  background-color: #e0e0e0;
  cursor: pointer;
}
.el-pagination.is-background.el-pager li.active {
  background-color: #13ce66;
  color: white;
}
.el-pagination button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 弹出层通用样式 */
.el-dialog {
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
.el-dialog.el-form {
  padding: 20px;
}
.el-dialog.el-form-item {
  margin-bottom: 20px;
}
.el-dialog.el-form-item label {
  font-weight: bold;
  color: #333;
}
.el-dialog.el-input {
  width: 100%;
  border-radius: 4px;
  border: 1px solid #ccc;
  padding: 8px 10px;
}
.el-dialog.avatar-uploader {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed #ccc;
  border-radius: 5px;
  width: 100px;
  height: 100px;
  margin: 0 auto;
  cursor: pointer;
}
.el-dialog.avatar-uploader img.avatar {
  width: 100%;
  height: 100%;
  border-radius: 5px;
  object-fit: cover;
}
.el-dialog.avatar-uploader i.el-icon-plus {
  font-size: 30px;
  color: #ccc;
}
.el-dialog.dialog-footer {
  display: flex;
  justify-content: flex-end;
  padding: 10px 20px;
}
.el-dialog.dialog-footer el-button {
  margin-left: 10px;
  border-radius: 4px;
  padding: 6px 12px;
  transition: all 0.3s ease;
}
.el-dialog.dialog-footer el-button:hover {
  opacity: 0.8;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}
</style>